﻿using RBAC.Common.infrastructures.interfaces;
using RBAC.Common.MYcontexts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.Common.infrastructures.Repostories
{
    public class BaseRepository<T> : IbaseRpository<T> where T : class, new()
    {



        EFMycontext db;

        public BaseRepository(EFMycontext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
           
            db.Set<T>().Add(entity);

            return db.SaveChanges();

        }
        /// <summary>
        /// 删除根据实体
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Deel(T item)
        {
            
            db.Set<T>().Remove(item);
            return db.SaveChanges();

        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Delete(List<T> t)
        {
           
            db.RemoveRange(t);

            return db.SaveChanges();

        }
        /// <summary>
        /// 删除根据id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteAll(int id)
        {
            var list = db.Set<T>().Find(id);
            db.RemoveRange(list);
            return db.SaveChanges();
        }

        /// <summary>
        /// 获取所有集合
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> Query()
        {
           return db.Set<T>().AsQueryable();
        } 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="ietm"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int update(T ietm)
        {
            db.Set<T>().Update(ietm);

            return db.SaveChanges();

        }
    }
}
